Imports System.Data.Common


Partial Class Presentation_Control_CreateExecutive
    Inherits System.Web.UI.Page
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        Dim Sql As String
        Dim Records As DbDataReader
        Dim FirstDepartment As New ListItem


        If Not Page.IsPostBack Then

            Page.Title = "Create Executive"

            Sql = "Select DepartmentID, DepartmentName FROM DepartmentMaster"
            Records = GenHelper.ExecuteReader(Sql, ConfigurationManager.ConnectionStrings.Item("MySqlConnString").ToString())

            FirstDepartment.Text = "Select a Department"
            FirstDepartment.Value = "0"
            FirstDepartment.Selected = True
            dlDepartments.DataTextField = "DepartmentName"
            dlDepartments.DataValueField = "DepartmentID"
            dlDepartments.DataSource = Records
            dlDepartments.DataBind()
            dlDepartments.Items.Add(FirstDepartment)
            Records.Close()

            Sql = "SELECT DISTINCT ExecutiveMaster.ExecutiveID, ExecutiveName FROM (ExecutiveMaster INNER JOIN (RoleMaster INNER JOIN DepartmentMaster ON DepartmentMaster.DepartmentID = RoleMaster.DepartmentID)" & _
            " ON ExecutiveMaster.ExecutiveID = RoleMaster.ExecutiveID)" & _
            " WHERE (DepartmentName = 'Administration' OR DepartmentName = 'Top Management') AND Active = 1" & _
            " ORDER BY ExecutiveName"
            Records = GenHelper.ExecuteReader(Sql, ConfigurationManager.ConnectionStrings.Item("MySqlConnString").ToString())

            dlExtraPerson.DataTextField = "ExecutiveName"
            dlExtraPerson.DataValueField = "ExecutiveID"
            dlExtraPerson.DataSource = Records
            dlExtraPerson.DataBind()
            Records.Close()


            If PreviousPage IsNot Nothing Then

                If PreviousPage.IsCrossPagePostBack Then
                    PostBack.Value = PreviousPage.GetUniqueID

                    Sql = "Select ExecutiveName, email, Designation, Location, Telephone, AccessID, DepartmentID, SupervisorID" & _
                        " FROM ExecutiveMaster WHERE ExecutiveID = " & PostBack.Value

                    Records = GenHelper.ExecuteReader(Sql, ConfigurationManager.ConnectionStrings.Item("MySqlConnString").ToString())

                    'Fill the fields

                    If Records.Read Then

                        txtName.Text = Records.Item("ExecutiveName").ToString
                        txtEmail.Text = Records.Item("email").ToString
                        txtDesignation.Text = Records.Item("Designation").ToString
                        txtLocation.Text = Records.Item("Location").ToString
                        txtTelephone.Text = Records.Item("Telephone").ToString
                        If Records.Item("AccessID") = 1 Then
                            cbAccess.Checked = False
                        Else
                            cbAccess.Checked = True
                        End If

                        dlDepartments.SelectedValue = Records.Item("DepartmentID")
                        dlExtraPerson.SelectedValue = Records.Item("SupervisorID")


                    End If

                    Records.Close()

                End If

            Else

                'Flag to show that the page is not a crosspage postback
                PostBack.Value = False

            End If


        End If

    End Sub

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click

        If PostBack.Value = False Then
            InsertExecutive()
        Else
            UpdateExecutive()
        End If


    End Sub

    Protected Sub InsertExecutive()

        Dim NewExecutive As Executive
        Dim Success As Boolean
        Dim Man As Integer

        If Not Page.IsValid Then
            lblSuccess.Visible = False
            Exit Sub
        End If

        If cbAccess.Checked Then
            Man = 2
        Else
            Man = 1
        End If

        'Submit the Parameters of the new executive

        NewExecutive = New Executive(txtName.Text, txtEmail.Text, txtDesignation.Text, txtTelephone.Text, txtLocation.Text, Man, dlDepartments.SelectedValue, dlExtraPerson.SelectedValue)

        Success = NewExecutive.Create()

        If Success = True Then
            lblSuccess.Text = "Successfully Saved"
            lblSuccess.Visible = True
            lblSuccess.ForeColor = Drawing.Color.Black
            txtName.Text = ""
            txtEmail.Text = ""
            txtDesignation.Text = ""
            txtTelephone.Text = ""
            txtLocation.Text = ""
            cbAccess.Checked = False
            dlDepartments.SelectedValue = 0
        Else
            lblSuccess.Text = "Not Saved"
            lblSuccess.Visible = True
            lblSuccess.ForeColor = Drawing.Color.Red
        End If

    End Sub

    Protected Sub UpdateExecutive()

        Dim ExecutiveToUpdate As Executive
        Dim ManAccess As Integer
        Dim Success As Boolean

        If cbAccess.Checked = True Then
            ManAccess = 2
        Else
            ManAccess = 1
        End If

        ExecutiveToUpdate = New Executive(PostBack.Value)
        Success = ExecutiveToUpdate.ChangeDetails(txtName.Text, txtEmail.Text, txtDesignation.Text, txtTelephone.Text, txtLocation.Text, ManAccess, dlDepartments.SelectedValue, dlExtraPerson.SelectedValue)

        'Code to clear the contents

        If Success = True Then

            lblSuccess.Text = "Successfully Saved"
            lblSuccess.Visible = True
            lblSuccess.ForeColor = Drawing.Color.Black
            txtName.Text = ""
            txtEmail.Text = ""
            txtDesignation.Text = ""
            txtTelephone.Text = ""
            txtLocation.Text = ""
            cbAccess.Checked = False
            dlDepartments.SelectedValue = 0
        Else
            lblSuccess.Text = "Not Saved"
            lblSuccess.ForeColor = Drawing.Color.Red
        End If

        'Put the page back into insert mode

        PostBack.Value = False

    End Sub

End Class
